<?php
/**
 * Created by JetBrains PhpStorm.
 * User: heimuya
 * Date: 14-1-23
 * Time: 下午8:28
 * 皇家鸡排后台函数库
 */


/**
 * 设置cookie
 * @param array/string $data 用户数据数组
 * @param string $cookieName 缓存名称
 */
function setCookieInfo($data, $cookieName){
    if(isset($data['passwd'])){//防止将用户密码信息进行缓存
        unset($data['passwd']);
    }
    if(is_array($data)){
        $str = http_build_query($data);//组合成query字符串
    }else{
        $str = $data;
    }
    import('ORG.Crypt.Base64');
    cookie($cookieName, Base64::encrypt($str, C('COOKIE_KEY')));
}

/**
 * 或者cookie
 * @param string $cookieName  缓存名称
 * @return
 */
function getCookieInfo($cookieName){
    import('ORG.Crypt.Base64');
    $cookie = Base64::decrypt(cookie($cookieName), C('COOKIE_KEY'));
    return (stripos($cookie, '&') == -1) ? $cookie : parse_str($cookie);
}

/**
 * 设置session
 * @param array $data session数据
 * @return bool
 */
function setSession($data){
    if(is_array($data)){
        foreach($data as $key => $value){
            session($key, $value);
        }
    }
}


/**
 * 将数组键值改为数字Id列
 */
function changeArrayKey($data, $col = 'id'){
	$temp = array();
	foreach ($data as $key => $value) {
		$temp[$value[$col]] = $value;
	}
	return $temp;
}
	
/**
 * 数据库json串解析函数，可以从另一个数组中找到对应Id的列，并将该列的值返回
 * @param $string 待解析json串
 * @param $data 数组
 */
function jsonStringDecode($string, $data){
	$str = json_decode($string, TRUE);
	$temp = array();
	foreach ($str as $key => $value) {
		if(!isset($data[$value]['visible']) || $data[$value]['visible'] == 'v'){
			$temp[$value] = $data[$value];
		}
	}
	return $temp;
 }


function random_num_str($str_length =  10) {  
	$randstr = ''; 
	for ($i = 0; $i < $str_length; $i++)  
	{  
		$randstr .= chr(mt_rand(48,57));  
	}  
	return $randstr; 
}